
import { request } from "../../request/index";
Page({
    data: {
        //左侧数据
        leftMenuList: [],
        rightContent: [],
        currentIndex: 0,
        scrollTop: 0
    },
    //接口返回数据
    Cates: [],

    onLoad: function(options) {

        //1.判断有没有旧的数据
        // {time:Data.now(),data[...]}
        //没有旧数据，发送请求
        //有的就使用本地存储

        //获取本地存储数据，小程con's't
        const Cates = wx.getStorageSync('cates');
        //判断
        if (!Cates) {
            this.getCates();
        } else {
            if (Date.now() - Cates.time > 1000 * 10) {
                this.getCates();
            } else {
                this.Cates = Cates.data;
                let leftMenuList = this.Cates.map(v => v.cat_name);
                let rightContent = this.Cates[0].children;
                this.setData({
                    leftMenuList,
                    rightContent
                })
            }
        }

    },

    async getCates() {
        const result = await request({ url: "/kinds/" });
        console.log(result);
        var data1 = result.t1
        var data2 =result.t2
        var data3 =result.t3
        data1.map((v1,index1)=>{
            v1.children=[];
            data2.map((v2,index2)=>{
            v2.children=[]
              if(v2.cat_tit===v1.cat_name){
                v1.children.push(v2)
              }
              data3.map((v3,index3)=>{
                if(v3.cat_tit===v2.cat_name){
                    v2.children.push(v3)
                  }
              })
            
          })
        })
        console.log(data1);
        this.Cates=data1;

        // 存到本地存储
        wx.setStorageSync('cates', { time: Date.now(), data: this.Cates })
        console.log(this.Cates);
        let leftMenuList = this.Cates.map(v => v.cat_name);
        let rightContent = this.Cates[0].children;
        this.setData({
            leftMenuList,
            rightContent
        })
    },
    handleItemTap(e) {
        //获取标题索引
        //给data中currentIndex值
        const { index } = e.currentTarget.dataset;
        let rightContent = this.Cates[index].children;
        this.setData({
                currentIndex: index,
                rightContent,
                scrollTop: 0
            })
            //scroll-view距离顶部
    },
})